﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class EmployeeRequestDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public EmployeeRequestDAL()
        {

        }
        public DataSet LoadEmployeeRequest(Nullable<int> Sequence, Nullable<int> EmployeeSequence, string RequestType, string Status, string SearchText)
        {
            string strSQL;

            DataSet ds = null;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@RequestType", RequestType);
                dbManager.AddParameters(2, "@EmployeeSequence", EmployeeSequence);
                dbManager.AddParameters(3, "@Status", Status);
                dbManager.AddParameters(4, "@SearchText", SearchText);
                dbManager.AddParameters(5, "@ActiveRecord", SystemInfo._ActiveRecordStatus);

                strSQL = "SELECT r.*"
                        + ", CONCAT(np.PREFIX_NAME, e.FIRST_NAME, ' ', e.LAST_NAME) AS FULL_NAME"
                        + ", e.SALARY"
                        + ", p.POS_REF, p.POS_NAME, c.CLASS_NAME, pj.JOB_SITE_NAME"
                    + " FROM HRM_ES_QUIT_REQUESTS r"
                        + " LEFT JOIN HRM_PS_EMPLOYEES e ON r.EMP_SEQ = e.EMP_SEQ"
                        + " LEFT JOIN HRM_MP_CLASSES c ON e.CLASS_SEQ = c.CLASS_SEQ"
                        + " LEFT JOIN HRM_MP_POSITIONS p ON e.POS_SEQ = p.POS_SEQ"
                        + " LEFT JOIN HRM_MP_JOB_SITES pj ON p.JOB_SITE_SEQ = pj.JOB_SITE_SEQ"
                        + " LEFT JOIN MAS_NAME_PREFIXES np ON e.NAME_PREFIX_SEQ = np.NAME_PREFIX_SEQ"
                    + " WHERE c.RECORD_STATUS = @ActiveRecord";
                if (!(SearchText.Trim() == ""))
                {
                    String whereSearchText =" AND  (e.FIRST_NAME LIKE CONCAT('%', @SearchText , '%')"
                            + " OR e.LAST_NAME LIKE CONCAT('%', @SearchText , '%')"
                        + ") ";
                }

                    

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);                

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }

            return ds;
        }
    }
}
